<script setup>
import { ref, computed, watch, toRefs } from 'vue'
import defaultImg from '../../../assets/fileicon/icon-default.png'
import pdfImg from '../../../assets/fileicon/icon-pdf.png'
import txtImg from '../../../assets/fileicon/icon-txt.png'
import wordImg from '../../../assets/fileicon/icon-word.png'
import xlsxImg from '../../../assets/fileicon/icon-xlsx.png'
// (/\.([^.]*)$/g.exec(data.documentName)?.[1] || '')
const props = defineProps({
  fitem: {
    type: Object,
    default: {}
  }
})
const showDetail = ref(false)
const { fitem } = toRefs(props)
const fileMap = new Map([
  ['0', defaultImg],
  ['pdf', pdfImg],
  ['txt', txtImg],
  ['doc', wordImg],
  ['docx', wordImg],
  ['xlsx', xlsxImg],
  ['xls', xlsxImg]
])
const sourceImg = computed(() => {
  const t = /\.([^.]*)$/g.exec(fitem.value?.documentName)?.[1] || '0'
  return fileMap.get(t)
})
</script>

<template>
  <div class="file-container">
    <img :src="sourceImg" alt="" />
    <div class="fileName" @click="showDetail = true">
      {{ fitem.documentName }}
    </div>
    <van-popup
      :show="showDetail"
      position="bottom"
      closeable
      round
      close-icon="close"
      @click-overlay="showDetail = false"
      @click-close-icon="showDetail = false"
      :style="{ height: '50%' }"
      teleport="body"
      class="popup-file"
    >
      <template #default>
        <div class="info">
          <div class="fileName info-title">{{ fitem.documentName }}</div>
          <div
            class="info-source"
            v-for="source in fitem.sources"
            :key="source.dataset_id"
          >
            <div v-html="source.content" class="content"></div>
          </div>
        </div>
      </template>
    </van-popup>
  </div>
</template>

<style lang="less" scoped>
.popup-file {
  .fileName {
    color: #1989fa;
    line-height: 0.3rem;
    cursor: pointer;
    white-space: nowrap;
    font-size: 0.26rem;
  }
  .info {
    padding: 0 0.2rem 0.2rem 0.2rem;
    height: calc(100% - 0.4rem);
    overflow-y: auto;
    .info-source {
      margin-bottom: 0.2rem;
      padding: 0.2rem;
      background-color: #f5f7fa;
      word-break: break-all;
      font-size: 0.26rem;
      line-height: 0.4rem;
      border-radius: 0.1rem;
      .content {
        :deep(img) {
          width: 100%;
        }
      }
    }
    .info-title {
      background-color: #fff;
      padding-right: 0.7rem;
      position: sticky;
      line-height: 1.2rem;
      top: 0;
      padding-right: 1rem;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
.file-container {
  display: flex;
  align-items: center;
  width: 100%;
  margin-bottom: 0.1rem;
  margin-right: 0.2rem;
  img {
    width: 0.3rem;
    height: 0.3rem;
    margin-right: 0.05rem;
  }
  .fileName {
    color: #1989fa;
    line-height: 0.3rem;
    cursor: pointer;
    white-space: nowrap;
  }
}
</style>
